﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HRM.Common
{
    public partial class TimSoBHXH : UserControl
    {

        private int _employeeId = 0;
        private string _employeeName = "";

        public override string Text
        {
            get { return txtSIBookNo_Tmp2.Text; }
            set { txtSIBookNo_Tmp2.Text = value; }
        }

        public string EmployeeName
        {
            get { return _employeeName; }
            set { _employeeName = value; }
        }

        public Int32 EmployeeId
        {
            get { return _employeeId; }
            set { _employeeId = value; }
        }

        public void SelectAll()
        {
            txtSIBookNo_Tmp2.SelectAll();
        }

        //
        public TimSoBHXH()
        {
            InitializeComponent();
        }

        private void txtSIBookNo_Tmp_TextChanged(object sender, EventArgs e)
        {
            if (txtSIBookNo_Tmp2.Focused)
            {
                if (txtSIBookNo_Tmp2.Text.Length > 0)
                {
                    AutoCompleteEmployee(txtSIBookNo_Tmp2.Text);
                    if (lstEmployee.Items.Count > 0)
                    {
                        lstEmployee.Visible = true;
                        lstEmployee.HeaderStyle = ColumnHeaderStyle.None;
                        lstEmployee.BringToFront();
                        lstEmployee.Items[0].Selected = true;
                    }
                    else
                        lstEmployee.Visible = false;
                }
                else
                    lstEmployee.Visible = false;
            }
        }

        void AutoCompleteEmployee(string Parameter)
        {
            lstEmployee.Items.Clear();
            DataTable dtGL = LocalDB.Employee_GetListByParameter(Parameter.ToLower());
            
            ListViewItem item;
            if (dtGL != null)
                for (int i = 0; i < dtGL.Rows.Count; i++)
                {
                    item = new ListViewItem(dtGL.Rows[i]["Employee_SIBookNo"].ToString());
                    item.SubItems.Add(dtGL.Rows[i]["FullName"].ToString());
                    item.Name = dtGL.Rows[i]["FullName"].ToString();
                    item.SubItems.Add(Convert.ToDateTime(dtGL.Rows[i]["Employee_DateOfBirth"]).ToString("dd/MM/yyyy"));
                    item.Tag = dtGL.Rows[i]["Employee_ID"].ToString();
                    item.SubItems.Add(dtGL.Rows[i]["Employee_IDCardNo"].ToString());
                    lstEmployee.Items.Add(item);
                }
        }

        private void txtSIBookNo_Tmp_Enter(object sender, EventArgs e)
        {
            txtSIBookNo_Tmp2.SelectAll();
        }

        private void txtSIBookNo_Tmp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (lstEmployee.Items.Count > 0)
                {
                    lstEmployee.Focus();
                    for (int i = 0; i < lstEmployee.Items.Count; i++)
                    {
                        if (lstEmployee.Items[i].Selected)
                        {
                            txtSIBookNo_Tmp2.Text = lstEmployee.Items[i].SubItems[0].Text;
                            //txtEmpName.Text = lstEmployee.Items[i].SubItems[0].Name;
                            //_MedicalPlace = Employee_GetOne(txtSIBookNo.Text, txtEmpName.Text).Rows[0][0].ToString();
                        }
                    }

                }
            }
            else if (e.KeyCode == Keys.Right && (txtSIBookNo_Tmp2.SelectionLength == txtSIBookNo_Tmp2.Text.Length || txtSIBookNo_Tmp2.SelectionStart == txtSIBookNo_Tmp2.Text.Length))
            {
                this.ProcessTabKey(true);
                ((TextBox)sender).SelectAll();
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Left && (txtSIBookNo_Tmp2.SelectionLength == txtSIBookNo_Tmp2.Text.Length || txtSIBookNo_Tmp2.SelectionStart == 0))
            {
                this.ProcessTabKey(false);
                ((TextBox)sender).SelectAll();
                e.Handled = true;
            }
        }

        private void lstEmployee_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //_listKeyUp = true;
                if (lstEmployee.Items.Count > 0)
                {
                    for (int i = 0; i < lstEmployee.Items.Count; i++)
                    {
                        if (lstEmployee.Items[i].Selected)
                        {
                            lstEmployee.Items[i].BackColor = Color.Yellow;
                            txtSIBookNo_Tmp2.Text = lstEmployee.Items[i].SubItems[0].Text;
                            //txtEmpName.Text = lstEmployee.Items[i].SubItems[0].Name;
                            //txtngaysinh.Text = lstEmployee.Items[i].SubItems[2].Text;
                            //txtCardNo.Text = lstEmployee.Items[i].SubItems[3].Text;
                            //txtEmpName.Tag = lstEmployee.Items[i].Tag;
                            EmployeeId = Globals.Object_GetValue<int>(lstEmployee.Items[i].Tag);
                            EmployeeName = lstEmployee.Items[i].Name;
                        }
                    }
                }
                lstEmployee.SendToBack();
                txtSIBookNo_Tmp2.Focus();
                lstEmployee.Visible = false;
            }
            //_listKeyUp = false;
        }

        private void lstEmployee_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            for (int i = 0; i < lstEmployee.Items.Count; i++)
            {
                if (lstEmployee.Items[i].Selected)
                {
                    txtSIBookNo_Tmp2.Text = lstEmployee.Items[i].SubItems[0].Text;
                    //txtEmpName.Text = lstEmployee.Items[i].SubItems[0].Name;
                    //txtEmpName.Tag = lstEmployee.Items[i].Tag;
                    EmployeeId = Globals.Object_GetValue<int>(lstEmployee.Items[i].Tag);
                    EmployeeName = lstEmployee.Items[i].Name;
                }
            }
            lstEmployee.SendToBack();
            txtSIBookNo_Tmp2.Focus();
            lstEmployee.Visible = false;
        }

        private void txtSIBookNo_Tmp2_Validated(object sender, EventArgs e)
        {

        }
    }
}
